import ValueWrapper from "../payload/ValueWrapper";
import ClientDevice from "../ClientDevice";
import {TopicFactory} from "../TopicFactory";
export default class SimDevice {
	constructor(){
    this.topicFactory = new TopicFactory();
    this.topicFactory.init();
  }
  init = (deviceBasicData) =>{
    this.mqttDevice = new ClientDevice(this.clientDataListener, deviceBasicData, {});
  }

  connectServer = () =>{
    this.mqttDevice.connectServer();
  }

  ping = () =>{
    this.mqttDevice.ping();
  }


  close() {
    this.mqttDevice.close();
  }

  /**
   * 属性推送
   * @param params
   */
  pushProperty(params) {
    const b = this.mqttDevice.pushPropertyValue(params);
    console.log("testPushProperty:" + b);
  }

  /**
   * 事件推送
   * @param identifier
   * @param params
   */
  pushEvent(identifier, params) {

    const b = this.mqttDevice.pushEvent(identifier, params);
    console.log("pushEvent:" + b);
  }
  //订阅
	subscribe=(pk, device)=> {
    let map = {
      "${productKey}": pk,
      "${deviceName}": device
    };

    let topics = [];
    topics.push(this.topicFactory.getTopic("PropertyPost", "PUSH", map));
    topics.push(this.topicFactory.getTopic("PropertySet", "PUSH", map));
    topics.push(this.topicFactory.getTopic("EventPost", "PUSH", map));
    topics.push(this.topicFactory.getTopic("Service", "PUSH", map));
    topics.push(this.topicFactory.getTopic("OTA_UPGRADE", "PUSH", map, 0));
    topics.push(this.topicFactory.getTopic("OTA_UPGRADE", "PUSH", map, 2));
    topics.push(this.topicFactory.getTopic("OTA_UPGRADE", "PUSH", map, 3));
    let subscribe1 = this.mqttDevice.subscribe(topics);
    // console.log("监听topic,", topics);
	}
  resSetPropertyValue = (payload) =>{
    this.mqttDevice.resSetPropertyValue(payload)
  }

  resService = (topic, payload) => {
    this.mqttDevice.resService(topic, payload)
  }

  clientDataListener = {

    onHandle: (topic, msg) =>{
      console.log("onHandle", topic, msg);
    },

    onSetProperty: (requestPayload)=> {
      console.log("=========================" + device.getDevId() + "=============================");
      let property = requestPayload.getProperty();
      for (let s in propertyInfoMap) {
        let valueWrapper = property[s];
        if (valueWrapper != null) {
          console.log("更改属性值:" + s + " " + valueWrapper.getValue());
        }
      }
      console.log("设备属性数据：" + JSON.stringify(propertyInfoMap));
      console.log("======================================================");

      let responsePayload = {
        id: requestPayload.getId(),
        code: 0
      };
      let b = this.mqttDevice.resSetPropertyValue(responsePayload);
      console.log("收到属性设置:" + JSON.stringify(requestPayload.getProperty()) + " 回复:" + b);
    },

    onInvokeService: (topic, payload) => {
      let map = {
        "回复数据": new ValueWrapper.IntValueWrapper(1)
      };

      let responsePayload = {
        data: map,
        method: payload.getMethod(),
        id: payload.getId(),
        code: 0
      };

      let b = this.mqttDevice.resService(topicFactory.getResService(topic), responsePayload);
      console.log("收到服务调用:" + JSON.stringify(payload) + " 回复:" + b);
    },

    onResPushProperty: function(payload) {
      console.log("收到属性推送回复:" + JSON.stringify(payload));
    },
	
    onResPushEvent: function(payload) {
      console.log("收到事件推送回复:" + JSON.stringify(payload));
    },
    onConnect(sucess){
      console.log("连接服务器:" + sucess);
    },
    onSubAck(sucess){
      console.log("监听topic反馈:" + sucess);
    },
    onPushAck(sucess){
      console.log("push反馈:" + sucess);
    },
    onClose(){
      console.log("连接关闭");
    }
  };


}
